﻿using System.Collections.Generic;
using System.Linq;
using Pay.Core;
using Pay.DTO;
using Pay.Service;

namespace Pay.Api.Controllers {
	public partial class OpenController : BaseController {
		/// <summary>
		/// 保存商品
		/// </summary>
		/// <returns>The product.</returns>
		/// <param name="model">Model.</param>
		public ApiResultMessage SaveProduct(product_model model) {
			if (model.id > 0) {
				var dto = _iProductService.Get(model.id);
				var merchant = _iMerchantService.Get(model.merchant_no);
				var oss_set = merchant.setting.oss_set;
				var oss = new AliyunOSS(oss_set.key, oss_set.secret, oss_set.endpoint);
				var image_urls = new List<string>();
				if (dto.image_path.Count > 0 && model.image_path.Count > 0) {
					//删除
					dto.image_path.Except(model.image_path).DataForEach(filter_id => {
						oss.DeleteObject(oss_set.bucket_name, filter_id.Trim('/'));
					});
				}
			}
			var result = _iProductService.Save(model);
			return ApiResultToJson.JsonData(result);
		}

		/// <summary>
		/// 商品列表
		/// </summary>
		/// <param name="query"></param>
		/// <returns></returns>
		public ApiPagedResult<product_dto> Products(goods_query query) {
			var model = query.ToDtoObject<GoodsQuery>();
			if (string.IsNullOrEmpty(query.bind_no)) {
				var oemer = _iMerchantService.Get(query.merchant_no);
				model.merchant_id = oemer.id;
			} else {
				var seller = _iMerchantService.Get(query.bind_no);
                model.sell_id = seller.id;
                //新增分销商获取商品
                if (seller.usertype == TO.UserType.distributor) {
                   var distribution_products = _iDistributionProductService.GetProducts(model);
                   return ApiResultToJson.Json(new ApiPagedResult<product_dto>(distribution_products.rows, distribution_products.total_result, distribution_products.page, distribution_products.size));
                }
			}
			var list = _iProductService.Get(model);
			var result = new ApiPagedResult<product_dto>(list.rows, list.total_result, list.page, list.size);
			return ApiResultToJson.Json(result);
		}

		/// <summary>
		/// 商品审核
		/// </summary>
		/// <returns>The audit.</returns>
		/// <param name="model">Model.</param>
		public ApiResultMessage Audit(product_audit_model model) {
			var result = _iProductService.Audit(model.id, model.audit_status, model.audit_reason);
			if (result >= 0) {
				return ApiResultToJson.Success("审核成功");
			}
			return ApiResultToJson.Fail("审核失败");
		}

		/// <summary>
		/// 保存商品分类
		/// </summary>
		/// <returns>The category.</returns>
		/// <param name="model">Model.</param>
		public ApiResultMessage SaveCategory(category_model model) {
			if (model.id > 0) {
				var dto = _iCategoryService.Get(model.id);
				var merchant = _iMerchantService.Get(model.merchant_no);
				var oss_set = merchant.setting.oss_set;
				var oss = new AliyunOSS(oss_set.key, oss_set.secret, oss_set.endpoint);
				var image_urls = new List<string>();
				if (dto.image_urls.Count > 0 && model.image_urls.Count > 0) {
					//删除
					dto.image_urls.Except(model.image_urls).DataForEach(filter_id => {
						oss.DeleteObject(oss_set.bucket_name, filter_id.Trim('/'));
					});
				}
			}
			var result = _iCategoryService.Save(model);
			return ApiResultToJson.JsonData(result);
		}

        /// <summary>
        /// 获取物流追踪信息
        /// </summary>
        /// <returns></returns>
        public ApiResultMessage GetLogistic(string order_no) {
            var order = _iOrderService.Get(order_no);
            var result = _iLogisticsDetailsService.GetList(order);
            return ApiResultToJson.JsonData(result);
        }
    }
}
